package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"starry_sky/internal/core"
	"starry_sky/internal/dto/dict-dto"
	"starry_sky/internal/model"
	"starry_sky/internal/service"
	"starry_sky/internal/service/dict-service"
	"starry_sky/internal/tools/logger"
)

func CreateDictType(c *gin.Context) {
	var dictType model.DictType
	err := c.ShouldBindJSON(&dictType)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	//保证创建均为可用
	dictType.Status = true
	err = service.Insert(&dictType)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "创建失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(dictType.ID))
}

func UpdateDictType(c *gin.Context) {
	var dictType model.DictType
	err := c.ShouldBindJSON(&dictType)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.Save(&dictType)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "更新失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func DeleteDictType(c *gin.Context) {
	var dictType model.DictType
	err := c.ShouldBindJSON(&dictType)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = dict_service.DeleteDictType(&dictType)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "删除失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func PageDictType(c *gin.Context) {
	var pageSupport core.PageSupport[model.DictType]
	err := c.ShouldBind(&pageSupport)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.FindPage(&pageSupport, map[string]any{}, make([]string, 0), "name")
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "查询失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(pageSupport))
}

func FindDictTypeById(c *gin.Context) {
	dictType := model.DictType{}
	err := c.ShouldBind(&dictType)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.FindById(&dictType)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "查询失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(dictType))
}

func CreateDictData(c *gin.Context) {
	var dictData model.DictData
	err := c.ShouldBindJSON(&dictData)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	//保证创建均为可用
	dictData.Status = true
	err = service.Insert(&dictData)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "创建失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(dictData.ID))
}
func UpdateDictData(c *gin.Context) {
	var dictData model.DictData
	err := c.ShouldBindJSON(&dictData)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.Save(&dictData)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "更新失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func FindDictDataById(c *gin.Context) {
	var dictData model.DictData
	err := c.ShouldBind(&dictData)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.FindById(&dictData)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "查询失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(dictData))
}

func DeleteDictData(c *gin.Context) {
	var dictData model.DictData
	err := c.ShouldBindJSON(&dictData)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.Delete(&dictData)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "删除失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func PageDictData(c *gin.Context) {
	var pageSupport dict_dto.DictDataPageSupport
	err := c.ShouldBind(&pageSupport)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.FindPage(&pageSupport.PageSupport, map[string]any{"dict_type": pageSupport.DictType}, make([]string, 0), "label")
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "查询失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(pageSupport))
}
